﻿/*
 * Crée par SharpDevelop.
 * Utilisateur: ngr
 * Date: 12/02/2015
 * Heure: 10:38
 * 
 * Pour changer ce modèle utiliser Outils | Options | Codage | Editer les en-têtes standards.
 */
using System;
using System.ComponentModel;
using System.Drawing;
using System.Linq.Expressions;
using System.Windows.Forms;

using Compta.Core.Classes;
using Compta.Core.Structures;
using Compta.Core;

namespace ComptaPerso.Controls
{
	/// <summary>
	/// Description of UC_ManageAccounts.
	/// </summary>
	public partial class UC_ManageAccounts : UserControl
	{
		
		ComptaCore core;
		Account acc;
		
		public UC_ManageAccounts(ComptaCore core, int AccountId)
		{
			//
			// The InitializeComponent() call is required for Windows Forms designer support.
			//
			InitializeComponent();
			
			//
			// TODO: Add constructor code after the InitializeComponent() call.
			//
			
			//txtbAccountName.Text = accountName;
			this.core = core;
			acc.Id = AccountId;
			
			LoadForm_Init();
			LoadForm_Bank();
			LoadForm_Devise();
			
		}
		
		
		void LoadForm_Init()
		{
			
			if (acc.Id == 0)
			{
				lblTitre1.Text = "Ajout d'un nouveau compte";
				txtbBalanceProjected.Enabled = false;
			}
			else{
				lblTitre1.Text = "Modification du compte n°" + acc.Id.ToString();
			}
		}
		
		// read all bank
		void LoadForm_Bank()
		{
			core.LoadAllBank();
			cbxBanque.Items.Clear();
			foreach(Bank b in core.ListBanks)
			{
				cbxBanque.Items.Add(b.Id.ToString() + " - " + b.Name);
			}
						
		}
		
		void LoadForm_Devise()
		{
			core.LoadAllDevise();
			cbxDevise.Items.Clear();
			foreach (Devise d in core.ListDevises)
			{
				cbxDevise.Items.Add(d.Name + " (" + d.Code + " - " + d.Symbole + " )");
			}
			
			
		}
		
		void SearchAccount(int id)
		{
			foreach (Account a in core.ListAccounts)
			{
				if (a.Id.Equals(id))
				{
					acc.Name = a.Name;
					acc.Bank = a.Bank;
					acc.BalanceReal = a.BalanceReal;
					acc.GetOperations();
					acc.Owner = a.Owner;
					acc.BalanceProjected = a.BalanceProjected;
					break;
				}
			}
		}
		
		
		void BtnCancelClick(object sender, EventArgs e)
		{
			this.Dispose();
			
		}
		
		void BtnApplyClick(object sender, EventArgs e)
		{
			
		}
		
		
		void BtnOkClick(object sender, EventArgs e)
		{
			AddAccount();
		}
		
		
		int Check()
		{
			int zret = 0;
			
			// banque
			if (cbxBanque.Text.Equals(""))
			{
				zret = 1;
			}
			
			// nom du compte
			if (zret == 0)
			{
				if (txtbAccountName.Text.Equals(""))
				{
					zret = 2;
				}
			}
			
			//titulaire
			if (zret == 0)
			{
				if (txtbAccountOwner.Text.Equals(""))
				{
					zret = 3;
				}
			}
			
			// devise
			if (zret == 0)
			{
				if (cbxDevise.Text.Equals(""))
				{
					zret = 4;
				}
			}
			
			// solde du compte
			if (zret == 0)
			{
				if (txtbBalance.Text.Equals(""))
				{
					zret = 5;
				}
				else{
					// teste de la numéricité du champs
					try
					{
						float result = (float)Convert.ToDecimal(txtbBalance.Text);
						
						MessageBox.Show(result.ToString());
					}
					catch
					{
						zret = 6;
					}

				}
				    
			}
			
			return zret;
		}
		
		void AddAccount()
		{
			bool b = false;
			// 1 - check la saisie
			int eval = Check();
			if (!eval.Equals(0))
			{
				string msg = "";
				switch (eval)
				{
					case 1:
						msg = "Il faut choisir un banque";
						break;
					case 2:
						msg = "Il faut donner un nom à votre compte";
						break;
					case 3:
						msg = "Il faut saisir le titulaire du compte";
						break;
					case 4:
						msg = "Il faut choisir une devise";
						break;
					case 5:
						msg = "Il n'y a pas de solde saisie";
						break;		
					case 6:
						msg = "Le solde saisie doit être numérique";
						break;	
				}
				MessageBox.Show(msg, "Erreur à la saisie");
			}
			else 
			{// 2 - si ok, on enr en base
				
				acc.Name = txtbAccountName.Text;			
				acc.BalanceReal = (float)Convert.ToDecimal(txtbBalance.Text);
				acc.BalanceProjected = acc.BalanceReal;
				acc.Bank_Id = Convert.ToInt32(cbxBanque.Text.Split('-')[0].Trim());
				acc.Owner = txtbAccountOwner.Text;
				string[] tsplit = cbxDevise.Text.Split('(');
				acc.Devise_Label = tsplit[0].Trim();
				tsplit = tsplit[1].Split('-');
				acc.Devise_Code = tsplit[0].Trim();
				acc.Devise_Symbole = tsplit[1].Trim();
				
				b = core.CreateAccount(acc);
			
			}
			
			
			if (b)
			{
				this.Dispose();
			}
		}
		
		
		
		/// <summary>
		/// ajout d'une banque
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		void BtnNewBankClick(object sender, EventArgs e)
		{
			
		}
		
		
		/// <summary>
		/// ajout d'une devise
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		void BtnNewDeviseClick(object sender, EventArgs e)
		{
			
		}
	}
}
